package com.ijiuchuang.jc_localization.presenter;

import android.content.Context;
import android.graphics.Bitmap;

import com.ijiuchuang.jc_localization.bean.cache.TagCache;
import com.ijiuchuang.jc_localization.bean.event.SocketGetInfoEvent;
import com.ijiuchuang.jc_localization.bean.lite.Station;
import com.ijiuchuang.jc_localization.contract.PositionContract;
import com.ijiuchuang.jc_localization.model.PositionModel;
import com.ijiuchuang.jc_localization.tools.utils.Log;

import java.util.List;

import rx.Subscriber;

/**
 * Created by Veev on 2016/6/30
 *
 * @link 384506557@qq.com
 */
public class PositionPresenter implements PositionContract.Presenter {

    private Context mContext;
    private PositionContract.View mView;
    private PositionContract.Model mModel;

    public PositionPresenter(Context mContext, PositionContract.View mView) {
        this.mContext = mContext;
        this.mView = mView;
        this.mModel = new PositionModel();

        mView.setPresenter(this);
    }

    @Override
    public void start() {
        // start here
        mView.initMap();
        loadConfig();
    }

    @Override
    public void loadConfig() {
        mModel.loadMap(mContext, new Subscriber<Bitmap>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(Bitmap bitmap) {
                mView.setMap(bitmap);
            }
        });

        mModel.loadStations(mContext, new Subscriber<List<Station>>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(List<Station> stations) {
                if (!stations.isEmpty()) {
                    for (Station ss : stations) {
                        mView.addStations(ss);
                    }
                }
            }
        });
    }

    @Override
    public void socketConnect() {
        mModel.socketConnect(new Subscriber<SocketGetInfoEvent>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(SocketGetInfoEvent socketGetInfoEvent) {
                mModel.locationProcess(socketGetInfoEvent, new Subscriber<TagCache>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(TagCache tagCache) {
                        Log.i(tagCache.toString());
                        mView.setLocationPoint(tagCache.pointF);
                        //mView.setLocationPoint(tagCache.name, tagCache.pointF);
                    }
                });
            }
        });
    }

    @Override
    public void getSocketList() {
        mView.showConnection(mModel.getSocketList());
    }
}